//
//  SPRollView.m
//  rollview
//
//  Created by 还带大道 on 2018/11/3.
//  Copyright © 2018年 还带大道. All rights reserved.
//

#import "SPRollView.h"
#import "SPWeakProxy.h"


@interface SPRollView()<UIScrollViewDelegate>
@property (nonatomic,weak) UIScrollView *scrollView;
@property (nonatomic,strong) NSMutableArray<UIButton *> *msgViews;
@property (nonatomic,strong) NSTimer *timer;

@end

@implementation SPRollView

+ (instancetype)shareInstance{
    static SPRollView *_instance = nil ;
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        if (_instance == nil) {
            _instance = [[SPRollView alloc] init];
        }
    });
    return _instance;
}


- (instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame]){
        
        _speed = 60;
        _timeInterval = 1.0/60;
        
        //add scrollView
        UIScrollView *scrollView = [UIScrollView new];
        scrollView.scrollEnabled = NO;
        scrollView.delegate = self;
//        scrollView.backgroundColor = [UIColor orangeColor];
        _scrollView = scrollView;
        [self addSubview:scrollView];
        
    }return self;
}


- (void)dealloc
{
    if(_timer){
        [self.timer invalidate];
        _timer = nil;
    }
    NSLog(@"---dealloc");
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    self.scrollView.frame = self.bounds;
    CGSize size = self.scrollView.frame.size;
    [self.msgViews enumerateObjectsUsingBlock:^(UIButton * _Nonnull view, NSUInteger idx, BOOL * _Nonnull stop) {
        [view sizeToFit];
        view.layer.cornerRadius = size.height * 0.5;
        CGFloat w = view.frame.size.width + 40;
        CGFloat x = self.margin_firstItem_x;
        if(idx == 0){
            x = self.margin_firstItem_x;
        }else{
             x = CGRectGetMaxX(self.msgViews[idx-1].frame) + self.margin_item_x;
        }
        view.frame = CGRectMake(x,0, w,size.height);
    }];
    self.scrollView.contentSize = CGSizeMake(CGRectGetMaxX(self.msgViews.lastObject.frame), 0);
}


#pragma mark--UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGPoint point = scrollView.contentOffset;
//    NSLog(@"contentOffset = %@",NSStringFromCGPoint(point));
    if(point.x >= scrollView.contentSize.width){
        scrollView.contentOffset = CGPointZero;
    }
}



- (void)startAnimation
{
    if(self.msgArray.count > 0){
        //立即启动定时器
        [self.timer setFireDate:[NSDate date]];
        NSLog(@"--->startAnimation");
    }
}


- (void)stopAnimation
{
    //暂停定时器
    [self.timer setFireDate:[NSDate distantFuture]];
    NSLog(@"--->stopAnimation");
}


#pragma mark--timerEvent
- (void)timerEvent
{
    [self horizontalRollAnimation];
}


- (void)horizontalRollAnimation
{
    //渐进动画
    [self.scrollView setContentOffset:CGPointMake(self.scrollView.contentOffset.x + self.speed * self.timeInterval, 0) animated:NO];
//    NSLog(@"RollView-->执行进动画---ContentOffset=%@",NSStringFromCGPoint(self.scrollView.contentOffset));
}


- (void)itemOnClick:(UIButton *)btn
{
    btn.selected = !btn.isSelected;
    NSLog(@"--->%ld",btn.tag);
}






#pragma mark-- getter&&setter

- (void)setMsgArray:(NSArray<NSString *> *)msgArray
{
    _msgArray = msgArray;
    [msgArray enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        UIButton *btn = [UIButton new];
        [btn setTitle:obj forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:15];
        btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [btn setImage:[UIImage imageNamed:@"icon_rollview_gift"] forState:UIControlStateNormal];
        btn.backgroundColor = RZ_COLOR(22, 20, 105);
        btn.imageEdgeInsets = UIEdgeInsetsMake(0,15,0,-15);
        btn.titleEdgeInsets = UIEdgeInsetsMake(0,25,0,-25);
//        [btn setBackgroundImage:[UIImage imageNamed:@"icon_rollview_bar"] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(itemOnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.scrollView addSubview:btn];
        [self.msgViews addObject:btn];
        btn.tag = idx;
//        if(idx % 2 == 0){
//            btn = [UIColor blueColor];
//        }else{
//            btn = [UIColor greenColor];
//        }
    }];
    [self layoutIfNeeded];
}


- (NSMutableArray<UIButton *> *)msgViews
{
    if(!_msgViews){
        _msgViews = [NSMutableArray array];
    }
    return _msgViews;
}


- (NSTimer *)timer
{
    if(!_timer){
        _timer = [NSTimer scheduledTimerWithTimeInterval:self.timeInterval target:[SPWeakProxy proxyWithTarget:self] selector:@selector(timerEvent) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
    }
    return _timer;
}


@end
